package sybel.pan;

import javax.swing.JPanel;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sybel.DOMPanelInterface;


public class PropertySet extends javax.swing.JPanel implements DOMPanelInterface {

    /** Creates new form PropertySet */
    public PropertySet(Node parent) {
        Parent = parent;
        NodeList nl = ((Element)parent).getElementsByTagName("propertySet");
        propertySet = (Element)nl.item(0);
        initComponents();
        if(propertySet != null)
            initPanel();
        annidation++;
    }

    public PropertySet(Node parent,int count) {
        Parent = parent;
        NodeList nl = ((Element)parent).getElementsByTagName("propertySet");
        propertySet = (Element)nl.item(0);
        initComponents();
        if(propertySet != null)
            initPanel();
        annidation = count +1;
        if(annidation == 3)
            jCBEntity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "entity","variable" }));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextProperty = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextValue = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jCBEntity = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Property Set"));

        jLabel1.setText("*property:");

        jLabel2.setText("value:");

        jLabel3.setText("Entity:");

        jCBEntity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "entity", "variable", "action execute", "property Get" }));
        jCBEntity.setMaximumSize(new java.awt.Dimension(59, 20));
        jCBEntity.setMinimumSize(new java.awt.Dimension(59, 20));
        jCBEntity.setPreferredSize(new java.awt.Dimension(59, 20));
        jCBEntity.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBEntityItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextProperty, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBEntity, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextValue, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextProperty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCBEntity, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCBEntityItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBEntityItemStateChanged
        EntitySelector();
    }//GEN-LAST:event_jCBEntityItemStateChanged

    public void initPanel(){
        jTextProperty.setText(propertySet.getAttribute("property"));
        jTextValue.setText(propertySet.getAttribute("value"));

        NodeList nl = propertySet.getElementsByTagName("entity");
        if(nl.item(0) != null)
            setEntityBody(1);

        nl = propertySet.getElementsByTagName("variable");
        if(nl.item(0) != null)
            setEntityBody(2);

        nl = propertySet.getElementsByTagName("actionExecute");
        if(nl.item(0) != null)
            setEntityBody(3);

        nl = propertySet.getElementsByTagName("propertyGet");
        if(nl.item(0) != null)
            setEntityBody(4);

        EntitySelector();
    }

    /**
     * Metodo per preimpostare il contenuto del pannello entity attraverso il passaggio di
     * un intero identificante il corpo del propertySet tra entity, variable, actionexecute e propertyGet
     * @param h passare: 1 per actionExecute, 2 propertyGet, 3 propertySet
     *
     */
    public void setEntityBody(int h){
        switch(h){
            case 1:// Entity
                jCBEntity.setSelectedIndex(h);
            case 2:// Variable
                jCBEntity.setSelectedIndex(h);
            case 3:// actionExecute
                jCBEntity.setSelectedIndex(h);
            case 4:// propertySet
                jCBEntity.setSelectedIndex(h);
            default:
                jCBEntity.setSelectedIndex(0);
        }
    }

    private void EntitySelector(){
        switch(jCBEntity.getSelectedIndex()){
            // Entity
            case 1:
                Entity entity;
                if(propertySet == null){
                    entity = new Entity(Parent);
                }else{
                    entity = new Entity(propertySet);
                }
                setEntityBody(entity);
                break;

             // Variable
            case 2:
                Variable variable;
                if(propertySet == null){
                    variable = new Variable(Parent);
                }else{
                    variable = new Variable(propertySet);
                }
                setEntityBody(variable);
                break;

            // Action Execute
            case 3:
                ActionExecute AEPanel;
                if(propertySet == null){
                    AEPanel = new ActionExecute(Parent,annidation);
                }else{
                    AEPanel = new ActionExecute(propertySet,annidation);
                }
                setEntityBody(AEPanel);
                break;

            // Property Get
            case 4:
                PropertyGet propertyget;
                if(propertySet == null){
                    propertyget = new PropertyGet(Parent,annidation);
                }else{
                    propertyget = new PropertyGet(propertySet,annidation);
                }
                setEntityBody(propertyget);
                break;
        }
    }

    private void setEntityBody(JPanel entity){
        jPanel1.removeAll();
        javax.swing.GroupLayout layout2 = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(layout2);
        layout2.setHorizontalGroup(
                layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
        layout2.setVerticalGroup(
                layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

        // rendo visibile il nuovo pannello
        jPanel1.repaint();
        jPanel1.setVisible(true);
    }

    public Node saveData(){
        Document doc = Parent.getOwnerDocument();
        propertySet = doc.createElement("propertySet");
        propertySet.setAttribute("property",jTextProperty.getText());
        propertySet.setAttribute("value",jTextValue.getText());

        int temp = jCBEntity.getSelectedIndex();
        switch(temp){
            case 1:// Entity
                Entity ent = (Entity)jPanel1.getComponents()[0];
                propertySet.appendChild(ent.saveData());
                break;

            case 2:// Variable
                Variable var = (Variable)jPanel1.getComponents()[0];
                propertySet.appendChild(var.saveData());
                break;

            case 3:// ActionExecute
                ActionExecute ae = (ActionExecute)jPanel1.getComponents()[0];
                propertySet.appendChild(ae.saveData());
                break;

            case 4:// PropertyGet
                PropertyGet propget = (PropertyGet)jPanel1.getComponents()[0];
                propertySet.appendChild(propget.saveData());
                break;
        }
        return (Node)propertySet;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCBEntity;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextProperty;
    private javax.swing.JTextField jTextValue;
    // End of variables declaration//GEN-END:variables
    private int annidation = 0;
    private Node Parent;
    private Element propertySet;
}
